package com.andnnl.mongo2;

import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;


public class Mobile2Codec implements Codec<Mobile2Entity> {
    public Mobile2Codec() {

    }

    /**
     * @see org.bson.codecs.Decoder#decode(BsonReader, DecoderContext)
     */
    @Override
    public Mobile2Entity decode(BsonReader reader, DecoderContext decoderContext) {
        Mobile2Entity user = new Mobile2Entity();
        reader.readStartDocument();
//        user.setId(reader.readObjectId("_id").toString());
        user.setMobile(reader.readInt64("_id"));
        user.setType(reader.readInt32("type"));

        reader.readEndDocument();
        return user;
    }

    /**
     * @see org.bson.codecs.Encoder#encode(BsonWriter, Object, EncoderContext)
     */
    @Override
    public void encode(BsonWriter writer, Mobile2Entity value, EncoderContext encoderContext) {
        writer.writeStartDocument();

        writer.writeInt64("_id", value.getMobile());
        writer.writeInt32("type", value.getType());
        writer.writeEndDocument();
    }

    /**
     * @see org.bson.codecs.Encoder#getEncoderClass()
     */
    @Override
    public Class<Mobile2Entity> getEncoderClass() {
        return Mobile2Entity.class;
    }
}  